// pages/tabBar/search/search.js 搜索页，即首页
const app = getApp();
import {request} from '../../../utils/util'
import {debounce} from '../../../utils/util' //防抖函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:"",  //输入框内容
    windowHeight:"", //窗口高度
    class:{         //样式控制
      search:"search",
      right:"right",
      tip:"tip-none",
      img:"img",
      clear:"clear",
      bottom:app.globalData.startBottom + "px",
      titleBottom:(app.globalData.startBottom + 70) + "px",
      tools:"tools",
      toolBottom:(app.globalData.startBottom - 90) + "px"
    },
    focus:false,  //是否聚焦
    searchList:[    //搜索提示列表
      
    ]
  },
  /**
   * 登陆进来后获取不能预览的类型
   */
  onShow(){
      request("GET","/api/service/no_preview/")
      .then(res =>{
        if(res.statusCode === 200){
          wx.setStorageSync('no_preview', res.data.data)
        }
      })
  },
  /**
   * 搜索框 focus事件
   */
  focus:function(e){
   
    const keyHeight = e.detail.height //键盘高度
    const newBottom = keyHeight - 40  //输入框离键盘的高度
    this.setData({
      ...this.data,
      class:{
        ...this.data.class,
        right:'right-show',
        search:"search-show",
        img:"img-active",
        tools:"tools-active",
        tip:"tip",
        bottom:newBottom + "px"
      },
      
    })
  },
  /**
   * 失去焦点事件
   */
  blur:function(){
    if(this.data.class.search === "search"){
      return
    }else{
      if(this.data.value.length > 0){
        this.setData({
          ...this.data,
          class:{
            ...this.data.class,
            tip:"tip-show",
            bottom:"5px"
          },
          
        })
      }else{
        this.setData({
          ...this.data,
          value:"",
          class:{
            ...this.data.class,
            right:"right",
            search:"search",
            clear:"clear",
            tip:"tip-none",
            img:"img",
            tools:"tools",
            bottom:app.globalData.startBottom + "px"
          },
          searchList:[]
        })
      }
      
    }
    
  },
  /**
   * 取消按钮
   */
  cancel:function(){
    
    this.setData({
      ...this.data,
      value:"",
      class:{
        ...this.data.class,
        right:"right",
        clear:"clear",
        search:"search",
        tip:"tip-none",
        img:"img",
        tools:"tools",
        bottom:app.globalData.startBottom + "px"
      },
      searchList:[],
      
    })
  },
  /**
   * 键盘输入变化，搜索的提示
   * @param {*} e 
   */
  input:debounce(function(e) {
    this.setData({
      ...this.data,
      value:e.detail.value
    })
    if(e.detail.value.length > 0){
      this.setData({
        ...this.data,
        class:{
          ...this.data.class,
          clear:"clear-show"
        },
      })
    }else{
      this.setData({
        ...this.data,
        class:{
          ...this.data.class,
          clear:"clear"
        },
      })
    }

  }, 
    function (e){
        if(this.data.value === ""){
          this.setData({
            ...this.data,
            searchList:[]
          })
        }else{
          request('GET',`/mock/31/api/search_tip?name=${this.data.value}`)
          .then((res) =>{
         
          this.setData({
            ...this.data,
            searchList:res.data.searchTip
          })
        })
        }      
    },300
    ),
  /**
   * 清除
   */
  clear:function(){
      this.setData({
        ...this.data,
        value:"",
        class:{
          ...this.data.class,
          right:"right",
          search:"search",
          clear:"clear",
          tip:"tip-none",
          img:"img",
          tools:"tools",
          bottom:app.globalData.startBottom + "px"
        },
        searchList:[]
      })
  },
  /**
   * 搜索按钮 
   * */ 
  doSearch:function(e){
    const name = e.detail.value
    wx.navigateTo({
      url: `../search/searchResult/searchReasult?name=${name}`,
    })
    
    this.setData({
      ...this.data,
      value:"",
      class:{
        ...this.data.class,
        right:"right",
        search:"search",
        clear:"clear",
        tip:"tip-none",
        img:"img",
        tools:"tools",
        bottom:app.globalData.startBottom + "px"
      },
      searchList:[]
    })
    
  },
  /**
   * 根据提示进入搜索结果页
   */
  goSearch:function(e){
      const name = e.currentTarget.dataset.text
      wx.navigateTo({
        url: `../search/searchResult/searchReasult?name=${name}`,
      })
  },
  /**
   * 剪贴板功能
   */
  handleForClipboard:function(){
          wx.getClipboardData({
            success(res) {
              let data = res.data
              // data = data.replace(/\ +/g,"")
              // data = data.replace(/[\r\n]/g,"")
              if(data.length > 255){
                data = data.substring(0,255)
              }
              if((/^\s+$/g).test(data) || data == "") {
                wx.showModal({
                  showCancel:"false",
                  content: "未检测到剪贴板存在内容",
                  success(res) {
                    if (res.confirm){
                       return
                    }
                  }
                })
              }else{
                wx.showModal({
                  title: '检测到剪切板内容\r\n是否共享至WEB消息通知栏',
                  content: data,
                  success(res) {
                    if (res.confirm) {
                      const userInfo = wx.getStorageSync('userInfo')
                      const param = {
                        org: userInfo.org,
                        sender: userInfo.id,
                        receiver: userInfo.id,
                        submit_action: "copy",
                        social_type: "微信",
                        message: data,
                        phone: userInfo.phone,
                      }
                      request('POST','/api/auth/message/',param,'',true,'正在连接卫星')
                      .then(res =>{
                        if (res.statusCode === 201) {
                          wx.showToast({
                            title: '发送成功',
                            icon: 'success',
                            duration: 1000
                          })
                        }
                      })
                    } else if (res.cancel) {
                      return
                    }
                  }
                })
              }
            },
            fail:res =>{
              console.log(res)
            }
          })
  },
  /*
  * 更多的按钮
  */
  handleForMore(){
    wx.showModal({
      showCancel:"false",
      content: "敬请期待",
      success(res) {
        if (res.confirm){
           return
        }
      }
    })
  }
})